package com.anhry.www.safety.manage.enterprise.struts.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpEconomyType;
import com.anhry.www.safety.manage.enterprise.bean.TbCorpType;
import com.anhry.www.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.www.safety.manage.system.bean.TbUserDept;
import com.anhry.www.safety.util.Loggers;

/**
 * @date:Jun 19, 2012
 * @remark:
 * @author:dongbin
 */
@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class CorpTypeAction extends EnterpriseBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public List typeList;
	public TbCorpType corpType;
	public String typeName;//类型名称
	public String typeParentId;//父id
	public String typeParentName;//父类型名称
	public String typeCode;//编码
	public String economyId;//对应国民经济类型id
	public String economyParentId;//对应国民经济类型id
	public String unitId;//主管部门（行业部门）
	private String id;
	private String pid;
	private List deptList;
	private List economyList;

	/**
	 * 菜单列表
	 * @return
	 */
	public String list(){
		try {
			this.setTypeList((List)this.utilService.getHqlQuery("from TbCorpType t where 1=1 and t.isDele=0 order by t.id"));
			return "list";
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("CorpTypeAction: list()方法异常："+e.getMessage());
		}
		return ERROR;
	}
	
	public String loadEdit(){
		try {
			List list = new ArrayList();
			List allDeptList = (List)this.utilService.getHqlQuery("from TbUserDept t where 1=1 and t.deptGrade=2 and (t.deptType=1 or t.deptType=2) order by t.deptType,t.id");
			for(int i=0;i<allDeptList.size();i++){
				TbUserDept dept = (TbUserDept)allDeptList.get(i);
				if("1".equals(dept.getDeptType()) && "3".equals(dept.getDeptClass()) && "2".equals(dept.getDeptGrade())){
					dept.setDeptName("安监局-"+dept.getDeptName());
					list.add(dept);
				}
				if("2".equals(dept.getDeptType()) && "2".equals(dept.getDeptGrade())){
					list.add(dept);
				}
			}
			this.setDeptList(list);
			this.setEconomyList((List)this.utilService.getHqlQuery("from TbCorpEconomyType t where 1=1 and t.typeParentId=0 order by t.id"));
			
			//如果是修改
			if(this.id != null && !"".equals(this.id)){
				TbCorpType type = (TbCorpType)this.utilService.getObjectById(TbCorpType.class, Long.parseLong(this.id));
				this.setCorpType(type);
			}
			//如果是添加子项
			if(this.pid != null && !"".equals(this.pid)){
				TbCorpType type = (TbCorpType)this.utilService.getObjectById(TbCorpType.class, Long.parseLong(this.pid));
				TbCorpType type1 = new TbCorpType();
				type1.setTypeParentId(type.getId());
				type1.setTypeParentName(type.getTypeName());
				this.setCorpType(type1);
			}
		} catch (Exception e) {
			Loggers.error("manager:CorpTypeAction:loadEdit()方法出错："+e);
			e.printStackTrace();
		}
		return "edit";
	}
	
	/**
	 * 加载企业国民经济表的小类
	 */
	public void loadEconomy(){
		this.contextPvd.getResponse().setCharacterEncoding("utf-8");
		StringBuffer opts = new StringBuffer();
		try {
			List<TbCorpEconomyType> list = (List)this.utilService.getHqlQuery("from TbCorpEconomyType t where 1=1 and t.typeParentId="+Long.parseLong(this.economyId)+"");
			if(list != null && list.size() > 0){
				opts.append("{");  //保存选项信息
				int counter = 0;                            //计数器
				for(TbCorpEconomyType type:list){
//					如果不是第一项，追加一个“,”用于分隔选项	{'id':'name','id1':'name1'}
	                if (counter > 0) {
	                    opts.append(",");
	                }
	                opts.append("'");
	                opts.append(type.getId());
	                opts.append("':'");
	                opts.append(type.getTypeName());
	                opts.append("'");
	                counter++;                      //计数器加1	
				}
				opts.append("}");
			}
			this.contextPvd.getResponse().getWriter().write(opts+"");
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			Loggers.error("manager:CorpTypeAction:loadEconomy()出错："+e);
			e.printStackTrace();
		}
	}
	
	/**
	 * 企业类型保存
	 * @return
	 */
	public String save(){
		TbCorpType obj = null;
		try {
			if(this.id != null && !"".equals(this.id)){	//修改
				obj = (TbCorpType)this.utilService.getObjectById(TbCorpType.class, Long.parseLong(this.id));
				if (null != obj) {
					if(null!=this.typeParentId && this.typeParentId.length()>0){
						TbCorpType parent = (TbCorpType)this.utilService.getObjectById(TbCorpType.class, Long.parseLong(this.typeParentId));
						if(parent != null){
							obj.setTypeParentId(Long.parseLong(this.typeParentId));
							obj.setTypeParentName(parent.getTypeName());
						}else{
							this.setTip("父节点不存在，保存失败！");
							return this.list();
						}
					}
					if(null!=this.economyId && this.economyId.length()>0){
						obj.setEconomyId(Long.parseLong(this.economyId));
					}
					if(null!=this.economyParentId && this.economyParentId.length()>0){
						obj.setEconomyParentId(Long.parseLong(this.economyParentId));
					}
					if(null!=this.unitId && this.unitId.length()>0){
						TbUserDept dept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.unitId));
						obj.setUnitId(Long.parseLong(this.unitId));
						obj.setUnitName(dept.getDeptName());
					}
					obj.setTypeName(this.typeName);
			        this.utilService.update(obj);
			        this.setTip("修改成功！");
				}
			}else{	//添加
				obj = new TbCorpType();
				if(null!=this.typeParentId && this.typeParentId.length()>0){
					TbCorpType parent = (TbCorpType)this.utilService.getObjectById(TbCorpType.class, Long.parseLong(this.typeParentId));
					if(parent != null){
						obj.setTypeParentId(Long.parseLong(this.typeParentId));
						obj.setTypeParentName(parent.getTypeName());
					}else{
						this.setTip("父节点不存在，保存失败！");
						return this.list();
					}
				}
				if(null!=this.economyId && this.economyId.length()>0){
					obj.setEconomyId(Long.parseLong(this.economyId));
				}
				if(null!=this.economyParentId && this.economyParentId.length()>0){
					obj.setEconomyParentId(Long.parseLong(this.economyParentId));
				}
				if(null!=this.unitId && this.unitId.length()>0){
					TbUserDept dept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.unitId));
					obj.setUnitId(Long.parseLong(this.unitId));
					obj.setUnitName(dept.getDeptName());
				}
				obj.setTypeName(this.typeName);
				obj.setIsDele("0");
		        this.utilService.save(obj);
		        this.setTip("添加成功！");
			}	
			return this.list();
		} catch (Exception e) {
			Loggers.error("manager:CorpTypeAction:save()方法出错："+e);
			e.printStackTrace();
		}				
		return ERROR;
	}

	public List getTypeList() {
		return typeList;
	}

	public void setTypeList(List typeList) {
		this.typeList = typeList;
	}

	public TbCorpType getCorpType() {
		return corpType;
	}

	public void setCorpType(TbCorpType corpType) {
		this.corpType = corpType;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public String getTypeParentId() {
		return typeParentId;
	}

	public void setTypeParentId(String typeParentId) {
		this.typeParentId = typeParentId;
	}

	public String getTypeParentName() {
		return typeParentName;
	}

	public void setTypeParentName(String typeParentName) {
		this.typeParentName = typeParentName;
	}

	public String getTypeCode() {
		return typeCode;
	}

	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}

	public String getEconomyId() {
		return economyId;
	}

	public void setEconomyId(String economyId) {
		this.economyId = economyId;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

	public List getEconomyList() {
		return economyList;
	}

	public void setEconomyList(List economyList) {
		this.economyList = economyList;
	}

	public String getEconomyParentId() {
		return economyParentId;
	}

	public void setEconomyParentId(String economyParentId) {
		this.economyParentId = economyParentId;
	}

}
